package com.yz.lzq.liwushuo.ui.modules.gift.top100;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.jsonbean.gift.GiftgoryTop100Bean;
import com.yz.lzq.liwushuo.ui.webview.category.CategoryWebActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by AdminJia on 2016/11/26.
 */

public class GiftTop100Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<GiftgoryTop100Bean.DataBean.ItemsBean> mList = null;
    private Context mContext;

    public List<GiftgoryTop100Bean.DataBean.ItemsBean> getmList() {
        return mList;
    }

    private View view = null;

    public GiftTop100Adapter(List<GiftgoryTop100Bean.DataBean.ItemsBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_gift_vp_top100, null);
        return new ViewHolderData(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GiftgoryTop100Bean.DataBean.ItemsBean itemsBean = mList.get(position);
        String name = itemsBean.getName();
        Log.i("TAG", "66666666666666666666onBindViewHolder: ");
        String short_description = itemsBean.getShort_description();
        final String url = itemsBean.getUrl();
        String price = itemsBean.getPrice();
        String cover_webp_url = itemsBean.getCover_image_url();
        Log.i("TAG", "----适配器onBindViewHolder:cover_webp_url " + cover_webp_url);
        if (holder instanceof ViewHolderData) {
            ((ViewHolderData) holder).simpleDraweeViewGiftVpTuiJianRecyclerView.setImageURI(Uri
                    .parse(cover_webp_url));
            ((ViewHolderData) holder).textViewGiftVpTop100RecyclerViewName.setText(name);
            ((ViewHolderData) holder).textViewGiftVpTop100RecyclerViewPrice.setText(price);
            ((ViewHolderData) holder).textViewGiftVpTop100RecyclerViewDescription.setText
                    (short_description);
            if (position <= 2) {

                ((ViewHolderData) holder).textViewGiftVpTop100RecyclerViewOrder

                        .setBackgroundColor(mContext.getResources().getColor(R.color.crimson));
            } else {
                ((ViewHolderData) holder).textViewGiftVpTop100RecyclerViewOrder

                        .setBackgroundColor(mContext.getResources().getColor(R.color.indianred));
            }
            ((ViewHolderData) holder).textViewGiftVpTop100RecyclerViewOrder.setText("top" +
                    (position + 1));

        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(mContext, CategoryWebActivity.class);
                intent.putExtra("url", url);
                mContext.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolderData extends RecyclerView.ViewHolder {
        @Bind(R.id.simpleDraweeView_top100_tuiJian_recyclerView1)
        SimpleDraweeView simpleDraweeViewGiftVpTuiJianRecyclerView;
        @Bind(R.id.textView_gift_vp_top100_recyclerView_order)
        TextView textViewGiftVpTop100RecyclerViewOrder;
        @Bind(R.id.textView_gift_vp_top100_recyclerView_description)
        TextView textViewGiftVpTop100RecyclerViewDescription;
        @Bind(R.id.textView_gift_vp_top100_recyclerView_name)
        TextView textViewGiftVpTop100RecyclerViewName;
        @Bind(R.id.textView_gift_vp_top100_recyclerView_price)
        TextView textViewGiftVpTop100RecyclerViewPrice;

        public ViewHolderData(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

//    public void addItems(List<GiftgoryTop100Bean.DataBean.ItemsBean> _list, int positionStart) {
//        mList.addAll(_list);
//        int itemCount = _list.size();
//        notifyItemRangeInserted(positionStart, itemCount);
//    }

    //    public void reloadRecyclerView(List<GiftgoryTop100Bean.DataBean.ItemsBean> _list, boolean
//            isClear) {
//        if (isClear) {
//            mList.clear();
//        }
//        mList.addAll(_list);
//        notifyDataSetChanged();
//    }
    //添加数据
    public void additems(List<GiftgoryTop100Bean.DataBean.ItemsBean> _list) {
        int positionStart = mList.size();
        mList.addAll(_list);
        notifyItemRangeChanged(positionStart, _list.size());
    }

    //刷新数据
    public void reLoadData(List<GiftgoryTop100Bean.DataBean.ItemsBean> _list, boolean isDelectOld) {
        if (isDelectOld) {
            mList.clear();
        }
        mList.addAll(_list);
        notifyDataSetChanged();
    }


    static class ViewHolder {


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}